package com.cm.cl;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import com.cm.cl.enums.Category;
import com.cm.cl.enums.Departments;
import com.cm.cl.enums.Emergency;
import com.cm.cl.enums.Process;
import com.cm.cl.enums.Source;
import javax.swing.JScrollPane;
import javax.swing.border.SoftBevelBorder;
import javax.swing.border.BevelBorder;

import org.apache.poi.hssf.util.HSSFColor.BLACK;
import javax.swing.table.DefaultTableModel;

public class Dlg_Query extends JDialog {

	/**
	 * 
	 */
	private final JPanel contentPanel = new JPanel();
	private JComboBox cmbEmg;
	private JComboBox cmbTaskCat;
	private JComboBox cmbSource;
	private JComboBox cmbCat;
	private JComboBox cmbDep;
	private JTextField txtFactor;
	private JTextField txtProposer;
	private JTextField txtHandler;
	private JTextField txtID;
	private JPanel pnlRS;
	private JTable table;
	JTable resultTable = null;
	Statement st = null;
	String sql = "";
	ResultSet rs = null;
	private JLabel label_6;

	private String prepareSQL() {
		StringBuilder sql = new StringBuilder("select * from `"
				+ Database.getTableName() + "` where 1");
		if (txtID.getText().length() > 0) {
			sql.append(" and id = '" + txtID.getText() + "'");
			return sql.toString();
		}
		if (!cmbCat.getSelectedItem().equals(Category.ALL)) {
			sql.append(" and category ='" + cmbCat.getSelectedItem().toString()
					+ "'");
		}
		if (!cmbDep.getSelectedItem().equals(Departments.ALL)) {
			sql.append(" and department ='"
					+ cmbDep.getSelectedItem().toString() + "'");
		}
		if (!cmbEmg.getSelectedItem().equals(Emergency.ALL)) {
			sql.append(" and emergency ='"
					+ cmbEmg.getSelectedItem().toString() + "'");
		}
		if (!cmbSource.getSelectedItem().equals(Source.ALL)) {
			sql.append(" and source ='"
					+ cmbSource.getSelectedItem().toString() + "'");
		}
		if (!cmbTaskCat.getSelectedItem().equals(Process.ALL)) {
			sql.append(" and task_category ='"
					+ cmbTaskCat.getSelectedItem().toString() + "'");
		}
		if (txtFactor.getText().length() > 0) {
			sql.append(" and factor = '" + txtFactor.getText() + "'");
		}
		if(txtHandler.getText().length()>0){
			sql.append(" and handler = '" +txtHandler.getText()+ "'");
		}
		if(txtProposer.getText().length()>0){
			sql.append(" and proposer = '" +txtProposer.getText()+ "'");
		}
		return sql.toString();
	}

	private void doQuery() throws SQLException {
		// TODO Auto-generated method stub
		/*
		 * if (rs != null) { rs.close(); } if (st != null) { st.close(); }
		 */
		sql = prepareSQL();
		System.out.println(sql);
		st = Database.getConnection().createStatement();
		rs = st.executeQuery(sql);
		rs.last();
		label_6.setText("查询结果:" + rs.getRow() + "项");
		// System.out.println("col:" + rs.getMetaData().getColumnCount() +
		// " row:"+ rs.getRow());

		resultTable = new ResultTable(new ResultSetTableModel(rs));
		// resultTable.setVisible(true);
		// System.out.println(resultTable.getValueAt(0, 0));
		for(int i=0 ; i< pnlRS.getComponentCount() ;i++){
			pnlRS.remove(i);
		}
		pnlRS.add(new JScrollPane(resultTable));
		// resultTable.setFillsViewportHeight(true);

	}

	public Dlg_Query() {

		setTitle("工单查询");
		setBounds(100, 100, 860, 604);
		getContentPane().setLayout(null);
		contentPanel.setBounds(0, 0, 580, 252);
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel);
		contentPanel.setLayout(null);
		{
			JLabel lblNewLabel = new JLabel("工单编号");
			lblNewLabel.setBounds(33, 38, 54, 15);
			contentPanel.add(lblNewLabel);
		}
		{
			JLabel lblNewLabel_1 = new JLabel("所在室");
			lblNewLabel_1.setBounds(33, 66, 54, 15);
			contentPanel.add(lblNewLabel_1);
		}

		cmbDep = new JComboBox();
		cmbDep.setModel(new DefaultComboBoxModel(Departments.values()));
		cmbDep.setBounds(109, 63, 166, 21);
		contentPanel.add(cmbDep);

		JLabel lblNewLabel_2 = new JLabel("问题分类");
		lblNewLabel_2.setBounds(320, 66, 54, 15);
		contentPanel.add(lblNewLabel_2);

		cmbCat = new JComboBox();
		cmbCat.setModel(new DefaultComboBoxModel(Category.values()));
		cmbCat.setBounds(384, 63, 154, 21);
		contentPanel.add(cmbCat);

		cmbSource = new JComboBox();
		cmbSource.setModel(new DefaultComboBoxModel(Source.values()));
		cmbSource.setBounds(109, 91, 166, 21);
		contentPanel.add(cmbSource);

		JLabel label = new JLabel("问题来源");
		label.setBounds(33, 94, 54, 15);
		contentPanel.add(label);

		JLabel label_1 = new JLabel("提出人");
		label_1.setBounds(320, 94, 54, 15);
		contentPanel.add(label_1);

		txtProposer = new JTextField();
		txtProposer.setBounds(384, 91, 154, 21);
		contentPanel.add(txtProposer);
		txtProposer.setColumns(10);

		txtHandler = new JTextField();
		txtHandler.setBounds(109, 122, 166, 21);
		contentPanel.add(txtHandler);
		txtHandler.setColumns(10);

		JLabel label_2 = new JLabel("提交人");
		label_2.setBounds(33, 125, 54, 15);
		contentPanel.add(label_2);

		cmbTaskCat = new JComboBox();
		cmbTaskCat.setModel(new DefaultComboBoxModel(Process.values()));
		cmbTaskCat.setBounds(109, 153, 166, 21);
		contentPanel.add(cmbTaskCat);

		JLabel label_3 = new JLabel("任务分类");
		label_3.setBounds(33, 156, 54, 15);
		contentPanel.add(label_3);

		JLabel label_4 = new JLabel("紧急程度");
		label_4.setBounds(320, 156, 54, 15);
		contentPanel.add(label_4);

		cmbEmg = new JComboBox();
		cmbEmg.setModel(new DefaultComboBoxModel(Emergency.values()));
		cmbEmg.setBounds(384, 153, 154, 21);
		contentPanel.add(cmbEmg);

		JLabel label_5 = new JLabel("工单系数");
		label_5.setBounds(33, 187, 54, 15);
		contentPanel.add(label_5);

		txtFactor = new JTextField();
		txtFactor.setBounds(109, 184, 166, 21);
		contentPanel.add(txtFactor);
		txtFactor.setColumns(10);

		label_6 = new JLabel("查询结果");
		label_6.setBounds(33, 212, 110, 15);
		contentPanel.add(label_6);

		txtID = new JTextField();
		txtID.setBounds(109, 35, 166, 21);
		contentPanel.add(txtID);
		txtID.setColumns(10);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setBounds(10, 523, 791, 33);
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane);
			{
				JButton okButton = new JButton("查询");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						try {
							doQuery();
							// Dlg_Query.this.pack();
							// Dlg_Query.this.repaint();
							Dlg_Query.this.validate();
						} catch (SQLException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}

					}

				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("取消");
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						Dlg_Query.this.dispose();
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}

		pnlRS = new JPanel();
		pnlRS.setBounds(28, 230, 763, 283);
		getContentPane().add(pnlRS);
		pnlRS.setLayout(new BorderLayout(0, 0));

	}
}
